# _*_ coding: UTF-8 _*_
# @Time     : 2021/11/3 14:18
# @Author   : 郑鲁明
# @Site     : http://www.hzdledu.cn/
# @File     : loging_page.py
# @Software : PyCharm
from selenium.webdriver.support.wait import WebDriverWait  # 等待类
from selenium.webdriver.support import expected_conditions  # 等待条件
from selenium.webdriver.common.by import By  # 定位符
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains


class LoginPage():
    # 定义类的属性并完成初始化
    def __init__(self, driver):
        # 账号输入框
        self.user_locator = (By.XPATH, '//*[@id="account"]/input')
        # 密码输入框
        self.pwd_locator = (By.XPATH, '//*[@id="password"]/input')
        # 验证码输入框
        self.vcode_locator = (By.XPATH, '//*[@id="authCode"]/div/input')
        # 登录按键
        self.login_locator = (By.XPATH, '//*[@id="root"]/div/div[1]/form/div[5]/div/div/span/button')

        self.driver = driver
        self.wait = WebDriverWait(driver, 30)
        # 调试使用
        # self.driver = webdriver.Chrome()

    # 页面操作方法
    # 输入账号
    def input_user(self, user):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.user_locator))
            self.driver.find_element(*self.user_locator).send_keys(user)
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/输入账号错误.png')
            raise e

    # 输入密码
    def input_password(self, pwd):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.pwd_locator))
            self.driver.find_element(*self.pwd_locator).send_keys(pwd)
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/输入密码错误.png')
            raise e

    # 输入验证码
    def input_vcode(self, vcode):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.vcode_locator))
            self.driver.find_element(*self.vcode_locator).send_keys(vcode)
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/输入验证码错误.png')
            raise e

    # 点击登录按键
    def click_login(self):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.login_locator))
            self.driver.find_element(*self.login_locator).click()
        except Exception as e:
            self.driver.get_screenshot_as_file('./log/点击登录错误')
            raise e


if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get('http://192.168.6.113/cloud/#/open/login')
    login_page = LoginPage(driver)

    login_page.input_user('admin')
    login_page.input_password('123456')
    login_page.input_vcode('8888')
    login_page.click_login()

    driver.quit()
